<?php

class sfFrontWebControllerDev extends sfWebController
{
  public function dispatch()
  {
    try
    {
      if (sfConfig::get('sf_logging_enabled'))
      {
        $this->dispatcher->notify(new sfEvent($this, 'application.log', array('Dispatch request')));
      }
      sfFilter::$filterCalled = array();
      $request    = $this->context->getRequest();
      $moduleName = $request->getParameter('module');
      $actionName = $request->getParameter('action');
      if (empty($moduleName) || empty($actionName))
      {
        throw new sfError404Exception(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).', $request->getPathInfo(), $moduleName, $actionName));
      }
      $this->forward($moduleName, $actionName);
    }
    catch (sfError404Exception $e) {
        $class = new ReflectionClass($moduleName.'Actions');
        $arMethods = array();
        foreach($class->getMethods() as $m) {
            if(
                substr($m->getName(),0,7) == 'execute' &&
                strlen($m->getName()) > 7
            ) {
                $methodName = substr($m->getName(),7);
                $methodName = strtolower($methodName[0]).substr($methodName,1);
                $arMethods[] = $methodName;
            }
        }
        echo "<h2>Erro:</h2>";
        echo '<h3>O método <b>'.$moduleName.'::'.$actionName.'</b> não existe.</h3>';
        echo 'Os que existem nesse modulo são:<br/><br/>';
        foreach($arMethods as $method) {
            echo " * ".$method."<br/>";
        }
        echo "<br/></br>";
        echo "<a class='button' href='javascript:void(0);' onclick='dev.createAction(\"".$moduleName."\",\"".$actionName."\")'>"
         ."criar o método <i>$actionName</i> em <i>".$moduleName."Actions.</i></a>";
    }
    catch (sfException $e)
    {
      $e->printStackTrace();
    }
    catch (Exception $e)
    {
      sfException::createFromException($e)->printStackTrace();
    }
  }
}
